﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Login.BLL;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace Courses
{
    public partial class Scheduling : Form
    {
        public Scheduling()
        {
            InitializeComponent();
        }
        //打印随机变量
        #region
        public static string strOne = "";
        public static string strTwo = "";
        public static string strThree = "";
        public static string strFour = "";
        public static string strFive = "";
        public static string strSix = "";
        public static string strSeven = "";
        public static string strEight = "";
        public static string strNine = "";
        public static string strTen = "";
        public static string strElven = "";
        public static string strTwelve = "";
        public static string strThirteen = "";
        public static string strFourteen = "";
        public static string strFifteen = "";
        public static string strSixteen = "";
        public static string strSeventeen = "";
        public static string strEightTeen = "";
        #endregion

        //数据库连接提取
        #region
        private static readonly string conter = ConfigurationManager.ConnectionStrings["consting"].ConnectionString;
        #endregion

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void Scheduling_Load(object sender, EventArgs e)
        {
            comboBox1.Text = "第一页";
            //提取排课数据表
            Bind();
            //总共打印页数放入下拉框中
            dropDown();
        }
        #endregion

        /// <summary>
        /// 总共打印页数放入下拉框中
        /// </summary>
        #region
        public void dropDown()
        {
            //搜索当前表共有多少行
            int d = biao.Rows.Count;
            try
            {
                //根据行数判断出有多少页
                if (d > 0)
                {
                    comboBox1.Items.Add("第一页");
                }
                if (d > 20)
                {
                    comboBox1.Items.Add("第二页");
                }
                if (d > 40)
                {
                    comboBox1.Items.Add("第三页");
                }
                if (d > 60)
                {
                    comboBox1.Items.Add("第四页");
                }
                if (d > 80)
                {
                    comboBox1.Items.Add("第五页");
                }
                if (d > 100)
                {
                    comboBox1.Items.Add("第六页");
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        #endregion

        /// <summary>
        /// 提取表的方法
        /// </summary>
        #region
        public void Bind()
        {
            //从数据库提取数据表
            DataSet ds = tb_ResultBLL.getData();
            //添加到当前窗体中
            biao.DataSource = ds.Tables[0];
        }
        #endregion

        /// <summary>
        /// 一键点击排课
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void btn_Click(object sender, EventArgs e)
        {
            //第十七周最少实验
            int shiYanLower = 100;
            int ChengGong = 1; //用于测试是否每个班级都排完
            tb_ResultBLL.chushi();  //清除原先所排课程
            btn1.Enabled = true;
            btn.Enabled = false;
            button1.Enabled = true;
            //进度条
            progressBar1.Visible = true;
            progressBar1.Maximum =15;
            progressBar1.Value = 0;
            progressBar1.Step = 1;
            //循环遍历各个时间点
            for (int i = 1; i <= 15; i++)
            {
                //赋值一个数
               ChengGong = 1;  
               int xunHuan = 0; //记录整个时间点尝试排了多少次
               while(ChengGong>0)
               {
                ChengGong = 0;
                //构造一个时间点所有班级数的sql语句
                string sql = "select count(*) from tb_NoClass where GroupId='{0}' and TimeId='{1}'";
                sql = string.Format(sql, "1", i);
                int a = tb_SweekBLL.postClassShu(sql); //当前时间所排班级数a
                //构造一个时间点所有班级的ID的SQL语句
                sql = "select ClassId from tb_NoClass where GroupId='{0}' and TimeId='{1}'";
                sql = string.Format(sql, "1", i);
                int[][] arr = new int[a][]; 
                int[] shuzu = new int[a];  //盛放各班级的实验数
                string[] shiyan = new string[a]; //盛放班级的ID
                int d = 0; //用于盛放多少个班级所上实验为奇数
                using (SqlConnection conn = new SqlConnection(conter))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    //二维数组每组的序号
                    int c = 0;
                    while (sdr.Read())
                    {
                        shiyan[c] = sdr[0].ToString();
                        int b = tb_SweekBLL.getShiYanShu(sdr[0].ToString());
                        //班级的实验数目
                        shuzu[c] = b;
                        if (b % 2 == 1)
                        {
                            d++;
                        }
                        arr[c] = new int[b];
                        //提取对应班级实验ID
                        arr[c] = tb_SweekBLL.getExpermentNumZu(sdr[0].ToString());
                        c++;
                    }
                    sdr.Close();
                }
                //用于盛放实验数为奇数的班级ID
                int[] BanJi = new int[d];
                int[] ZhouZero = new int[d];
                int g = 0;  //盛放实验数为奇数数组的序号
                for (int f = 0; f < a; f++)
                {
                    if (shuzu[f] % 2 == 1)
                    {
                        BanJi[g] = Convert.ToInt32(shiyan[f]);
                        g++;
                    }
                }
                //无论实验数为奇数还是偶数都当做偶数排
                PaiKe(shuzu, arr, a, shiyan, ZhouZero, BanJi, d);
                //循环遍历重新拍实验数为奇数的班级
                for (int Try = 0; Try < 3; Try++)
                {
                    for (int k = 0; k < d; k++)
                    {
                        if (BanJi[k] != 0)
                        {
                            //判断实验数为奇数的班级是否排成功
                            if (!tb_SweekBLL.TiaoZheng(BanJi[k], ZhouZero[k], i))
                            {
                                ChengGong++;
                            }
                            else
                            {
                                BanJi[k] = 0; ZhouZero[k] = 0;
                            }
                        }
                    }
                }
                tb_SweekBLL.YaSuo(i);  //将第十七周尽量缩减
                tb_SweekBLL.ZhiHuan(i);  //将各周列里的最少项置换到第十七周
                tb_SweekBLL.YaSuo(i);  //将第十七周尽量缩减
                   //循环遍历每个班是否都排完了
                for (int c = 0; c < a; c++)
                {
                    if (shuzu[c] != 0)
                    {
                        ChengGong++;
                    }
                }
                //判断第十七周是否还有实验
                int panDuanShiYanNum = tb_SweekBLL.panDuanShiYan(Convert.ToString(i));
                if (xunHuan<8)
                {
                    //判断所剩实验数是否小于上次最少实验数
                    if (shiYanLower > panDuanShiYanNum)
                    {
                        shiYanLower = panDuanShiYanNum; 
                    }
                    if (panDuanShiYanNum != 0)
                    {
                        ChengGong++;
                    }
                }
                if (xunHuan>=8  && xunHuan<=16)
                {
                    if (panDuanShiYanNum <= shiYanLower)
                    {
                        break;
                    }
                }
                if (xunHuan>16)
                 {
                     break;
                 }
                xunHuan++;
               }
               //判断这时间点排课是否成功，如果不成功暂停排课
               if (ChengGong !=0)
               {
                   break; 
               }
               progressBar1.Value += progressBar1.Step;
            }
            progressBar1.Visible = false;
            btn.Enabled = true;
            tb_SweekBLL.FourLie();  //调换列使列更美观
            Bind();
            if (ChengGong==0)
            {
                MessageBox.Show("恭喜！排课成功！");
            }
            else
            {
                MessageBox.Show("排课失败！");   
            }
        }
        /// <summary>
        /// 一次排课
        /// </summary>
        /// <param name="shuzu">盛放各班级的实验数</param>
        /// <param name="arr">盛放班级对应的实验</param>
        /// <param name="a">当前时间所排班级数</param>
        /// <param name="shiyan">盛放班级的ID</param>
        /// <param name="ZhouZero">实验数为奇数的班级对应的列只有一个实验数组</param>
        /// <param name="BanJi">盛放实验数为奇数的班级ID</param>
        /// <param name="t">盛放多少个班级所上实验为奇数</param>
        public void  PaiKe(int[] shuzu,int[][] arr,int a,string[] shiyan,int[] ZhouZero,int[] BanJi,int t)
        {
            if (a >= 2)
            {
                Random r = new Random();
                //随机打乱班级对应实验的顺序
                if (shuzu[0] >= 4)
                {  int m = r.Next(shuzu[0] - 2);      int shiyanId = arr[0][m];    arr[0][m] = arr[0][shuzu[0] - 1];    arr[0][shuzu[0] - 1] = shiyanId;      m = r.Next(shuzu[0] - 2);    shiyanId = arr[0][m];    arr[0][m] = arr[0][shuzu[0] - 2];      arr[0][shuzu[0] - 2] = shiyanId;
                }
                if (shuzu[1] >= 4)
                {     int m = r.Next(shuzu[1] - 2);      int shiyanId = arr[1][m];        arr[1][m] = arr[1][shuzu[1] - 1];       arr[1][shuzu[1] - 1] = shiyanId;     m = r.Next(shuzu[1] - 2);    shiyanId = arr[1][m];    arr[1][m] = arr[1][shuzu[1] - 2];      arr[1][shuzu[1] - 2] = shiyanId;
                }
                if (shuzu[a-1] >= 4)
                {  int m = r.Next(shuzu[a-1] - 2);   int shiyanId = arr[a-1][m];     arr[a-1][m] = arr[a-1][shuzu[a-1] - 1];    arr[a-1][shuzu[a-1] - 1] = shiyanId;   m = r.Next(shuzu[a-1] - 2);  shiyanId = arr[a-1][m];  arr[a-1][m] = arr[a-1][shuzu[a-1] - 2];  arr[a-1][shuzu[a-1] - 2] = shiyanId;
                }
                if (shuzu[a - 2] >= 4)
                {    int m = r.Next(shuzu[a - 2] - 2);  int shiyanId = arr[a - 2][m];     arr[a - 2][m] = arr[a - 2][shuzu[a - 2] - 1];  arr[a - 2][shuzu[a - 2] - 1] = shiyanId;     m = r.Next(shuzu[a - 2] - 2);   shiyanId = arr[a - 2][m];    arr[a - 2][m] = arr[a - 2][shuzu[a - 2] - 2];     arr[a - 2][shuzu[a - 2] - 2] = shiyanId;
                }
            }
            //将实验周一分为二，循环遍历其中一半
            for (int k = 2; k < 10; k++)
            {         
                int[] gmm = new int[2 * a];
                int h = 1;     
                for (int n = 0; n < a; n++)   
                {   
                    //判断周序号以及另一半对应的周序号的列中是否有约束
                    int d = tb_SweekBLL.PaiKeFou(shiyan[n],k);
                    if (d != 0)
                    {               } 
                    else  {    
                        if (a == 1 && (k == 3 || k == 5) && shuzu[n] <= 6)     { 
                        }
                            else       
                        {     
                            int m = 0;
                            // 二次判断当前列中是否与所排班级的实验相互冲突的
                            int garem = PaiKeOne(shuzu, arr, n, gmm, a);  
                            //如果该班级实验数为奇数
                            if (garem == 2 && shuzu[n] % 2 == 1)
                            {
                                for (int i = 0; i < t; i++)
                                {
                                    if (BanJi[i] == Convert.ToInt32(shiyan[n]))
                                    {
                                        ZhouZero[i] = k;
                                    }
                                }
                                PaiKeTwo(shuzu, arr, n, gmm, a, shiyan, k, h, m, garem);
                                if (n == a - k)
                                {
                                    n = n + 1;
                                }
                            }
                            else
                            {
                                if (k % a == 1)
                                {
                                    if (n == 0)
                                    {
                                    }
                                    else
                                    {
                                        PaiKeThree(shuzu, arr, n, gmm, a, shiyan, k, h, m);
                                    }
                                }
                                else
                                {
                                    PaiKeThree(shuzu, arr, n, gmm, a, shiyan, k, h, m); if (n == a - k) { n = n + 1; }
                                }
                            }
                            }
                    }
                }
            }
        }
        /// <summary>
        /// 二次判断当前列中是否与所排班级的实验相互冲突的
        /// </summary>
        /// <param name="shuzu">盛放各班级的实验数</param>
        /// <param name="arr">盛放班级对应的实验</param>
        /// <param name="n">当前班级的序号</param>
        /// <param name="gmm">当前列中已有的实验组</param>
        /// <param name="a">当前时间所排班级数</param>
        /// <returns></returns>
        public int PaiKeOne(int[] shuzu, int[][] arr, int n, int[] gmm, int a)
        {
            int b = 0; //有多少个实验在该列中找不到
            for (int i = 0; i < shuzu[n]; i++)
            {
                for (int c = 0; c < 2*a; c++)
                {
                    if (arr[n][i]==gmm[c])
                    {
                        b++;
                    }
                }
            }
            //如果有两个以上的实验在该列中找不到
            if (shuzu[n]-b>=2)
            {
                return 1;
            }
            else
            {
                //如果只有一个实验在该列中找不到
                if (shuzu[n] - b >= 1)
                {
                    return 2; 
                }
                else
               {
                   return 0;
                }
            }
        }

        public void PaiKeThree(int[] shuzu, int[][] arr, int n, int[] gmm, int a, string[] shiyan, int k, int h, int m)
        {
            int garem = PaiKeOne(shuzu, arr, n, gmm, a);
            if (garem == 1)
                {
                    PaiKeTwo(shuzu, arr, n, gmm, a, shiyan, k, h, m,garem);
                }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="shuzu">盛放各班级的实验数</param>
        /// <param name="arr">盛放班级对应的实验</param>
        /// <param name="n">当前班级的序号</param>
        /// <param name="gmm">当前列中已有的实验组</param>
        /// <param name="a">当前时间所排班级数</param>
        /// <param name="shiyan">盛放班级的ID</param>
        /// <param name="k">周序号</param>
        /// <param name="h">判断为第几组的序号</param>
        /// <param name="m">班级对应的实验序号，初始为0</param>
        /// <param name="garem">garem个实验在该列中找不到</param>
        public void PaiKeTwo(int[] shuzu, int[][] arr, int n, int[] gmm, int a, string[] shiyan, int k, int h, int m,int garem)
        {
            if (shuzu[n] != 0)
            {
                if (shuzu[n]%2==1 || garem==1)
                {
                    int shiyanId = 0;
                    shiyanId = arr[n][m];
                    //判断列中已有的实验组是否含有该实验
                    for (int i = 0; i < 2 * a; i++)
                    {
                        if (gmm[i] == shiyanId)
                        {
                            m++;
                            PaiKeTwo(shuzu, arr, n, gmm, a, shiyan, k, h, m, garem);
                            return;
                        }
                    }
                    string zhou = "Week" + k;
                    //将该实验添加到列中已有是实验组中，一遍下一个班级做对比
                    if (h == 1)
                    {
                        gmm[n] = shiyanId;
                    }
                    else
                    {
                        gmm[a + n] = shiyanId;
                    }
                    string BanJiMing = shiyan[n];
                    //判断如果第一组没排就排第一组，否则排第二组
                    if (h == 1)
                    {
                        tb_SweekBLL.shangKeZhouCunQu(zhou, shiyanId, BanJiMing, 1);
                        int c = k + 8;
                        zhou = "Week" + c;
                        tb_SweekBLL.shangKeZhouCunQu(zhou, shiyanId, BanJiMing, 2);
                        h++;
                        //删除数组中已排完的实验
                        deleteShu(arr, m, shuzu[n], n);
                        shuzu[n] = shuzu[n] - 1;
                        //嵌套
                        PaiKeTwo(shuzu, arr, n, gmm, a, shiyan, k, h, m, garem);
                    }
                    else
                    {
                        if (h == 2)
                        {
                            tb_SweekBLL.shangKeZhouCunQu(zhou, shiyanId, BanJiMing, 2);
                            int c = k + 8;
                            zhou = "Week" + c;
                            tb_SweekBLL.shangKeZhouCunQu(zhou, shiyanId, BanJiMing, 1);
                            deleteShu(arr, m, shuzu[n], n);
                            shuzu[n] = shuzu[n] - 1;
                        }
                        else
                        {

                        }
                    }
                }
            }
        }

        #endregion

        /// <summary>
        /// 删除数组数
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="a"></param>
        /// <param name="n"></param>
        /// <param name="b"></param>
        #region
        public void deleteShu(int[][] arr,int a,int n,int b)
        {
            for (int i = a; i < n-1; i++)
            {
                arr[b][i]=arr[b][i+1];
            }
        }
        #endregion

        /// <summary>
        /// 打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void btn1_Click(object sender, EventArgs e)
        {
            try
            {
                //判断打印的是第几页
                if (comboBox1.Text == "第一页")
                {
                    printPreviewDialog2.ShowDialog();
                }
                if (comboBox1.Text == "第二页")
                {
                    printPreviewDialog1.ShowDialog();
                }
                if (comboBox1.Text == "第三页")
                {
                    printPreviewDialog3.ShowDialog();
                }
                if (comboBox1.Text == "第四页")
                {
                    printPreviewDialog4.ShowDialog();
                }
                if (comboBox1.Text == "第五页")
                {
                    printPreviewDialog5.ShowDialog();
                }
                if (comboBox1.Text == "第六页")
                {
                    printPreviewDialog6.ShowDialog();
                }
            }
            catch { }
        }
        //打印第一页方法
        private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            printDocument2.DefaultPageSettings.Landscape = true;
            printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("name", 1000, 600);
            dayin(20,"第     一     页",0,e);
        }
        //打印第二页方法
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            printDocument1.DefaultPageSettings.Landscape = true;
            printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("name", 1000, 600);
            dayin(40,"第     二     页",20,e);
        }
        //打印第三页方法
        private void printDocument3_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            printDocument3.DefaultPageSettings.Landscape = true;
            printDocument3.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("name", 1000, 600);
            dayin(60,"第     三     页",40,e);
        }
        //打印第四页方法
        private void printDocument4_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            printDocument4.DefaultPageSettings.Landscape = true;
            printDocument4.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("name", 1000, 600);
            dayin(80, "第     四     页", 60, e);
        }
        //打印第五页方法
        private void printDocument5_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            printDocument5.DefaultPageSettings.Landscape = true;
            printDocument5.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("name", 1000, 600);
            dayin(80, "第     五     页", 60, e);
        }
        //打印第六页方法
        private void printDocument6_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            printDocument6.DefaultPageSettings.Landscape = true;
            printDocument6.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("name", 1000, 600);
            dayin(80, "第     六     页", 60, e);
        }
        //打印方法
        #region
        public void dayin(int i, string ye, int n, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //当前页多出行f默认为0
            int MoreRows = 0;
            //提取数据表
            DataSet dt = tb_ResultBLL.getData();
            biao.DataSource = dt.Tables[0];
            //数据表行数d
            int d = dt.Tables[0].Rows.Count;
            if (i > d)
            {
                //当前页多出行赋值
                MoreRows = i - d;
                //真实行i
                i = d;
            }
            //页宽
            int printWidth = e.PageBounds.Width;
            //页高
            int printHeight = e.PageBounds.Height;
            int left = printWidth / 2 - 260;
            int right = printWidth / 2 + 507;
            int top = printHeight / 2 - 470;
            Brush myBrush = new SolidBrush(Color.Black);
            Pen mypen = new Pen(Color.Black);
            e.Graphics.DrawString(ye, new Font("宋体", 10, FontStyle.Bold), myBrush, new Point(printWidth / 2 + 250, top + 690));
            int b = 70;
            int s = 40;
            //绘图
            for (int c = 0; c < 21 - MoreRows; c++)
            {
                e.Graphics.DrawLine(mypen, left + 140, top + 40, left + 140, top + b);
                e.Graphics.DrawLine(mypen, left + 195, top + 40, left + 195, top + b);
                e.Graphics.DrawLine(mypen, left + 250, top + 40, left + 250, top + b);
                e.Graphics.DrawLine(mypen, left + 305, top + 40, left + 305, top + b);
                e.Graphics.DrawLine(mypen, left + 360, top + 40, left + 360, top + b);
                e.Graphics.DrawLine(mypen, left + 415, top + 40, left + 415, top + b);
                e.Graphics.DrawLine(mypen, left + 470, top + 40, left + 470, top + b);
                e.Graphics.DrawLine(mypen, left + 525, top + 40, left + 525, top + b);
                e.Graphics.DrawLine(mypen, left + 580, top + 40, left + 580, top + b);
                e.Graphics.DrawLine(mypen, left + 635, top + 40, left + 635, top + b);
                e.Graphics.DrawLine(mypen, left + 690, top + 40, left + 690, top + b);
                e.Graphics.DrawLine(mypen, left + 755, top + 40, left + 755, top + b);
                e.Graphics.DrawLine(mypen, left + 820, top + 40, left + 820, top + b);
                e.Graphics.DrawLine(mypen, left + 885, top + 40, left + 885, top + b);
                e.Graphics.DrawLine(mypen, left + 950, top + 40, left + 950, top + b);
                e.Graphics.DrawLine(mypen, left + 1015, top + 40, left + 1015, top + b);
                e.Graphics.DrawLine(mypen, left + 1080, top + 40, left + 1080, top + b);
                e.Graphics.DrawLine(mypen, left+140, top + s, left + 1080, top + s);
                e.Graphics.DrawLine(mypen, left+140, top + s + 30, left + 1080, top + s + 30);
                b = b + 30;
                s = s + 30;
            }
            //加标题
            e.Graphics.DrawLine(mypen, left, top + 40, left, top + 40 + 30 * (21 - MoreRows));
            e.Graphics.DrawLine(mypen, left + 80, top + 40, left + 80, top + 40 + 30 * (21 - MoreRows));
            e.Graphics.DrawLine(mypen, left, top + 40, left+140, top + 40);
            e.Graphics.DrawLine(mypen, left, top + 70, left+140, top + 70);
            e.Graphics.DrawLine(mypen, left, top + 40 + 30 * (21 - MoreRows), left + 140, top + 40 + 30 * (21 - MoreRows));
            e.Graphics.DrawString("实验时间", new Font("宋体", 11), myBrush, new Point(left + 2, top + 46));
            e.Graphics.DrawString("班级", new Font("宋体", 12), myBrush, new Point(left + 2 + 80, top + 46));
            e.Graphics.DrawString("组别", new Font("宋体", 12), myBrush, new Point(left + 2 + 140, top + 46));
            e.Graphics.DrawString("第二周", new Font("宋体", 10), myBrush, new Point(left + 2 + 195, top + 46));
            e.Graphics.DrawString("第三周", new Font("宋体", 10), myBrush, new Point(left + 2 + 250, top + 46));
            e.Graphics.DrawString("第四周", new Font("宋体", 10), myBrush, new Point(left + 2 + 305, top + 46));
            e.Graphics.DrawString("第五周", new Font("宋体", 10), myBrush, new Point(left + 2 + 360, top + 46));
            e.Graphics.DrawString("第六周", new Font("宋体", 10), myBrush, new Point(left + 2 + 415, top + 46));
            e.Graphics.DrawString("第七周", new Font("宋体", 10), myBrush, new Point(left + 2 + 470, top + 46));
            e.Graphics.DrawString("第八周", new Font("宋体", 10), myBrush, new Point(left + 2 + 525, top + 46));
            e.Graphics.DrawString("第九周", new Font("宋体", 10), myBrush, new Point(left + 2 + 580, top + 46));
            e.Graphics.DrawString("第十周", new Font("宋体", 10), myBrush, new Point(left + 2 + 635, top + 46));
            e.Graphics.DrawString("第十一周", new Font("宋体", 10), myBrush, new Point(left + 2 + 690, top + 46));
            e.Graphics.DrawString("第十二周", new Font("宋体", 10), myBrush, new Point(left + 2 + 755, top + 46));
            e.Graphics.DrawString("第十三周", new Font("宋体", 10), myBrush, new Point(left + 2 + 820, top + 46));
            e.Graphics.DrawString("第十四周", new Font("宋体", 10), myBrush, new Point(left + 2 + 885, top + 46));
            e.Graphics.DrawString("第十五周", new Font("宋体", 10), myBrush, new Point(left + 2 + 950, top + 46));
            e.Graphics.DrawString("第十六周", new Font("宋体", 10), myBrush, new Point(left + 2 + 1015, top + 46));
            int a = 80;
            int m = n;
            //向所绘的框中添加内容
            for (n = m; n < i; n++)
            {
                strOne = dt.Tables[0].Rows[n][2].ToString();
                strTwo = dt.Tables[0].Rows[n][0].ToString();
                strThree = dt.Tables[0].Rows[n][1].ToString();
                strFour = dt.Tables[0].Rows[n][3].ToString();
                if (strFour == "不排课") { strFour = ""; }
                strFive = dt.Tables[0].Rows[n][4].ToString();
                if (strFive == "不排课") { strFive = ""; }
                strSix = dt.Tables[0].Rows[n][5].ToString();
                if (strSix == "不排课") { strSix = ""; }
                strSeven = dt.Tables[0].Rows[n][6].ToString();
                if (strSeven == "不排课") { strSeven = ""; }
                strEight = dt.Tables[0].Rows[n][7].ToString();
                if (strEight == "不排课") { strEight = ""; }
                strNine = dt.Tables[0].Rows[n][8].ToString();
                if (strNine == "不排课") { strNine = ""; }
                strTen = dt.Tables[0].Rows[n][9].ToString();
                if (strTen == "不排课") { strTen = ""; }
                strElven = dt.Tables[0].Rows[n][10].ToString();
                if (strElven == "不排课") { strElven = ""; }
                strTwelve = dt.Tables[0].Rows[n][11].ToString();
                if (strTwelve == "不排课") { strTwelve = ""; }
                strThirteen = dt.Tables[0].Rows[n][12].ToString();
                if (strThirteen == "不排课") { strThirteen = ""; }
                strFourteen = dt.Tables[0].Rows[n][13].ToString();
                if (strFourteen == "不排课") { strFourteen = ""; }
                strFifteen = dt.Tables[0].Rows[n][14].ToString();
                if (strFifteen == "不排课") { strFifteen = ""; }
                strSixteen = dt.Tables[0].Rows[n][15].ToString();
                if (strSixteen == "不排课") { strSixteen = ""; }
                strSeventeen = dt.Tables[0].Rows[n][16].ToString();
                if (strSeventeen == "不排课") { strSeventeen = ""; }
                strEightTeen = dt.Tables[0].Rows[n][17].ToString();
                if (strEightTeen == "不排课") { strEightTeen = ""; }
                if (n > 0)
                {
                    if (dt.Tables[0].Rows[n][2].ToString() == dt.Tables[0].Rows[n - 1][2].ToString())
                    {
                        strOne = "";
                    }
                    if (dt.Tables[0].Rows[n][2].ToString() != dt.Tables[0].Rows[n - 1][2].ToString())
                    {
                        e.Graphics.DrawLine(mypen, left, top + a - 10, left + 80, top + a - 10);
                    }
                }
                e.Graphics.DrawString(strOne, new Font("宋体", 9), myBrush, new Point(left + 2, top + a));
                if (n>0)
                {
                    if (dt.Tables[0].Rows[n][0].ToString()==dt.Tables[0].Rows[n-1][0].ToString())
                    {
                        strTwo = "";
                    }
                    if (dt.Tables[0].Rows[n][0].ToString() != dt.Tables[0].Rows[n - 1][0].ToString())
                    {
                        e.Graphics.DrawLine(mypen, left+80, top + a -10, left + 140, top + a -10);
                    }   
                }
                e.Graphics.DrawString(strTwo, new Font("宋体", 9), myBrush, new Point(left + 2 + 80, top + a));
                e.Graphics.DrawString(strThree, new Font("宋体", 9), myBrush, new Point(left + 2 + 140, top + a));
                e.Graphics.DrawString(strFour, new Font("宋体", 9), myBrush, new Point(left + 2 + 195, top + a));
                e.Graphics.DrawString(strFive, new Font("宋体", 9), myBrush, new Point(left + 2 + 250, top + a));
                e.Graphics.DrawString(strSix, new Font("宋体", 9), myBrush, new Point(left + 2 + 305, top + a));
                e.Graphics.DrawString(strSeven, new Font("宋体", 9), myBrush, new Point(left + 2 + 360, top + a));
                e.Graphics.DrawString(strEight, new Font("宋体", 9), myBrush, new Point(left + 2 + 415, top + a));
                e.Graphics.DrawString(strNine, new Font("宋体", 9), myBrush, new Point(left + 2 + 470, top + a));
                e.Graphics.DrawString(strTen, new Font("宋体", 9), myBrush, new Point(left + 2 + 525, top + a));
                e.Graphics.DrawString(strElven, new Font("宋体", 9), myBrush, new Point(left + 2 + 580, top + a));
                e.Graphics.DrawString(strTwelve, new Font("宋体", 9), myBrush, new Point(left + 2 + 635, top + a));
                e.Graphics.DrawString(strThirteen, new Font("宋体", 9), myBrush, new Point(left + 2 + 690, top + a));
                e.Graphics.DrawString(strFourteen, new Font("宋体", 9), myBrush, new Point(left + 2 + 755, top + a));
                e.Graphics.DrawString(strFifteen, new Font("宋体", 9), myBrush, new Point(left + 2 + 820, top + a));
                e.Graphics.DrawString(strSixteen, new Font("宋体", 9), myBrush, new Point(left + 2 + 885, top + a));
                e.Graphics.DrawString(strSeventeen, new Font("宋体", 9), myBrush, new Point(left + 2 + 950, top + a));
                e.Graphics.DrawString(strEightTeen, new Font("宋体", 9), myBrush, new Point(left + 2 + 1015, top + a));
                a = a + 30;
            }
        }
         #endregion
        #endregion

        /// <summary>
        /// 导入Excel表格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void button1_Click(object sender, EventArgs e)
        {
            //文件选择类
            OpenFileDialog fd = new OpenFileDialog();
            //判断是否选中文件
            if (fd.ShowDialog() == DialogResult.OK)
            {
                button1.Enabled = false;       
                string fileName = fd.FileName;
                //导出文件
                Export(fileName);
            } 
        }
        /// <summary>
        /// 导出文件
        /// </summary>
        /// <param name="fileName"></param>
        private void Export(string fileName)
        {   
            //连接excel文档
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +  "Data Source=" + fileName + ";" +  "Extended Properties='Excel 8.0;HDR=Yes;IMEX=0'";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            int n = biao.RowCount; //表中所有行数
            OleDbCommand cmd = new OleDbCommand();
            string shiJian; string banJi; string zu; string sql;
            string weekTwo; string weekThree; string weekFour; string weekFive; string weekSix; string weekSenven; string weekEight; string weekNine; string weekTen; string weekEleven; string weekTwelve; string weekThirteen; string weekFourteen; string weekFifteen; string weekSixteen;
            try
            {
                //循环遍历所有行
                for (int i = 0; i < n; i++)
                {
                    //提取行的时间
                    shiJian = biao.Rows[i].Cells[0].Value.ToString();
                    //提取行的班级名
                    banJi = biao.Rows[i].Cells[1].Value.ToString();
                    //提取行的组名
                    zu = biao.Rows[i].Cells[2].Value.ToString();
                    //清除空为不排课
                    weekTwo = biao.Rows[i].Cells[3].Value.ToString();
                    if (weekTwo == "不排课") { weekTwo = ""; } weekThree = biao.Rows[i].Cells[4].Value.ToString();
                    if (weekThree == "不排课") { weekThree = ""; } weekFour = biao.Rows[i].Cells[5].Value.ToString();
                    if (weekFour == "不排课") { weekFour = ""; } weekFive = biao.Rows[i].Cells[6].Value.ToString();
                    if (weekFive == "不排课") { weekFive = ""; } weekSix = biao.Rows[i].Cells[7].Value.ToString();
                    if (weekSix == "不排课") { weekSix = ""; }
                    weekSenven = biao.Rows[i].Cells[8].Value.ToString(); if (weekSenven == "不排课") { weekSenven = ""; }
                    weekEight = biao.Rows[i].Cells[9].Value.ToString(); if (weekEight == "不排课") { weekEight = ""; }
                    weekNine = biao.Rows[i].Cells[10].Value.ToString(); if (weekNine == "不排课") { weekNine = ""; }
                    weekTen = biao.Rows[i].Cells[11].Value.ToString(); if (weekTen == "不排课") { weekTen = ""; }
                    weekEleven = biao.Rows[i].Cells[12].Value.ToString(); if (weekEleven == "不排课") { weekEleven = ""; }
                    weekTwelve = biao.Rows[i].Cells[13].Value.ToString(); if (weekTwelve == "不排课") { weekTwelve = ""; }
                    weekThirteen = biao.Rows[i].Cells[14].Value.ToString(); if (weekThirteen == "不排课") { weekThirteen = ""; }
                    weekFourteen = biao.Rows[i].Cells[15].Value.ToString(); if (weekFourteen == "不排课") { weekFourteen = ""; }
                    weekFifteen = biao.Rows[i].Cells[16].Value.ToString(); if (weekFifteen == "不排课") { weekFifteen = ""; }
                    weekSixteen = biao.Rows[i].Cells[17].Value.ToString(); if (weekSixteen == "不排课") { weekSixteen = ""; }
                    //构造导入Excel表的语句
                    sql = "insert into [Sheet1$](上课时间,班级,班级分组,第二周,第三周,第四周,第五周,第六周,第七周,第八周,第九周,第十周,第十一周,第十二周,第十三周,第十四周,第十五周,第十六周)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}')";
                    sql = string.Format(sql, shiJian, banJi, zu, weekTwo, weekThree, weekFour, weekFive, weekSix, weekSenven, weekEight, weekNine, weekTen, weekEleven, weekTwelve, weekThirteen, weekFourteen, weekFifteen, weekSixteen);
                    cmd = new OleDbCommand(sql, conn);
                    //导入Excel
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            catch (Exception err)
            {
                conn.Close();
                MessageBox.Show("操作失败！" + err.ToString());
            }
        }
        #endregion

        /// <summary>
        /// 表格点击设定不上课周事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void biao_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //提取选中行中的班级名称
            string banJi = Convert.ToString(biao.SelectedRows[0].Cells[0].Value.ToString());
            // 搜索班级的Id
            string banJiID = tb_SweekBLL.getClassId(banJi);
            //设定不上课周界面实例化
            tb_zhidingNoweek zhuan = new tb_zhidingNoweek();
            zhuan.id = banJiID;
            zhuan.ShowDialog();
            Bind();
        }
        #endregion


    }
}
